package com.it.sun.easychat.framework.security.handler;

import cn.hutool.http.ContentType;
import com.alibaba.fastjson.JSON;
import com.it.sun.easychat.model.util.ResultHelper;
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author sunmannian
 * @ClassName com.it.sun.easychat.framework.security.handler.LoginFailureHandler
 * @Date 2020/5/24 20:12
 * @Version V1.0
 * Description  <登陆失败处理器>
 * Copyright (C) 北京中电兴发科技有限公司  since 2020/5/24
 */
public class LoginFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response,
                                        AuthenticationException exception)
            throws IOException {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        response.setContentType(ContentType.JSON.getValue());
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.getWriter().write(JSON.toJSONString(ResultHelper.getFailed(
                HttpStatus.UNAUTHORIZED.value(), exception.getMessage(), null)));
    }
}
